/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.codefactory.geneticalgorithm.view;

import com.codefactory.geneticalgorithm.HibernateUtil;
import com.codefactory.geneticalgorithm.MachineInfo;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author kurbatov
 */
public class MainJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public MainJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        typeLabel = new javax.swing.JLabel();
        typeComboBox = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        intensityLabel = new javax.swing.JLabel();
        intensityFTField = new javax.swing.JFormattedTextField();
        intensityMeasureLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        owerhaulTimeLabel = new javax.swing.JLabel();
        overhaulTimeFTField = new javax.swing.JFormattedTextField();
        overhaulTimeMeasureLabel = new javax.swing.JLabel();
        overhaulIntencityLabel = new javax.swing.JLabel();
        overhaulIntencityFTField = new javax.swing.JFormattedTextField();
        overhaulIdleLabel = new javax.swing.JLabel();
        overhaulIdleFTField = new javax.swing.JFormattedTextField();
        maintenanceTimeLabel = new javax.swing.JLabel();
        maintenanceTimeFTField = new javax.swing.JFormattedTextField();
        maintenanceTimeMeasureLabel = new javax.swing.JLabel();
        maintenanceIntencityLabel = new javax.swing.JLabel();
        maintenanceIntencityFTField = new javax.swing.JFormattedTextField();
        maintenanceIdleLabel = new javax.swing.JLabel();
        maintenanceIdleFTField = new javax.swing.JFormattedTextField();
        jSeparator3 = new javax.swing.JSeparator();
        addButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jFTFrepairCosts = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jFTFMaintenanceCosts = new javax.swing.JFormattedTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        nameLabel.setText("Наименование");

        typeLabel.setText("Вид оборудования");

        typeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Электротехническое", "Котельное и теплосиловое", "Компрессорное и насосное", "Трубопроводы наружние", "Трубопроводы внутренние", "Воздуховоды", "Фильтры" }));
        typeComboBox.setSelectedIndex(-1);
        typeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeComboBoxActionPerformed(evt);
            }
        });

        intensityLabel.setText("Трудоёмкость");

        intensityFTField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        intensityMeasureLabel.setText("чел./час на 100 чел./час");

        owerhaulTimeLabel.setText("Наработка до кап. ремонта");

        overhaulTimeFTField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        overhaulTimeFTField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        overhaulTimeMeasureLabel.setText("часов");

        overhaulIntencityLabel.setText("Трудоёмкость");

        overhaulIntencityFTField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        overhaulIntencityFTField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        overhaulIntencityFTField.setToolTipText("");

        overhaulIdleLabel.setText("Время простоя");

        overhaulIdleFTField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        overhaulIdleFTField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        maintenanceTimeLabel.setText("Наработка до тек. ремонта");

        maintenanceTimeFTField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        maintenanceTimeFTField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        maintenanceTimeMeasureLabel.setText("часов");

        maintenanceIntencityLabel.setText("Трудоёмкость");

        maintenanceIntencityFTField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        maintenanceIntencityFTField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        maintenanceIntencityFTField.setToolTipText("");

        maintenanceIdleLabel.setText("Время простоя");

        maintenanceIdleFTField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        maintenanceIdleFTField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        addButton.setText("Добавить");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Стоимость кап. ремонта");

        jFTFrepairCosts.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        jFTFrepairCosts.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel2.setText("Стоимость тек. ремонта");

        jFTFMaintenanceCosts.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        jFTFMaintenanceCosts.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jMenu1.setText("Расписание");

        jMenuItem1.setText("Показать");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Очистить");
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(typeLabel)
                            .addComponent(nameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(typeComboBox, 0, 459, Short.MAX_VALUE)
                            .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(intensityLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(intensityFTField, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(intensityMeasureLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(owerhaulTimeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(overhaulTimeFTField, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(overhaulTimeMeasureLabel)
                        .addGap(18, 18, 18)
                        .addComponent(overhaulIntencityLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(overhaulIntencityFTField, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(overhaulIdleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(overhaulIdleFTField, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(maintenanceTimeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(maintenanceTimeFTField, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(maintenanceTimeMeasureLabel)
                        .addGap(18, 18, 18)
                        .addComponent(maintenanceIntencityLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(maintenanceIntencityFTField, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(maintenanceIdleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(maintenanceIdleFTField, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 475, Short.MAX_VALUE)
                        .addComponent(addButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFTFrepairCosts, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFTFMaintenanceCosts, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeLabel)
                    .addComponent(typeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(intensityLabel)
                    .addComponent(intensityFTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(intensityMeasureLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(owerhaulTimeLabel)
                    .addComponent(overhaulTimeFTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(overhaulTimeMeasureLabel)
                    .addComponent(overhaulIntencityLabel)
                    .addComponent(overhaulIntencityFTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(overhaulIdleLabel)
                    .addComponent(overhaulIdleFTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maintenanceTimeLabel)
                    .addComponent(maintenanceTimeFTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(maintenanceTimeMeasureLabel)
                    .addComponent(maintenanceIntencityLabel)
                    .addComponent(maintenanceIntencityFTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(maintenanceIdleLabel)
                    .addComponent(maintenanceIdleFTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jFTFrepairCosts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFTFMaintenanceCosts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(addButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void typeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeComboBoxActionPerformed
        String workRate = "";
        if (typeComboBox.getSelectedIndex() == 0) {
            workRate = "7";
        }
        if (typeComboBox.getSelectedIndex() == 1) {
            workRate = "10";
        }
        if (typeComboBox.getSelectedIndex() == 2) {
            workRate = "9";
        }
        if (typeComboBox.getSelectedIndex() == 3) {
            workRate = "9";
        }
        if (typeComboBox.getSelectedIndex() == 4) {
            workRate = "5";
        }
        if (typeComboBox.getSelectedIndex() == 5) {
            workRate = "3";
        }
        if (typeComboBox.getSelectedIndex() == 6) {
            workRate = "13";
        }
        intensityFTField.setText(workRate);
        try {
            intensityFTField.commitEdit();
        } catch (ParseException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_typeComboBoxActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        MachineInfo machineInfo = new MachineInfo();
        String msg = null;
        if (typeComboBox.getSelectedIndex() == -1) {
            msg = ("Выберите тип оборудования!");
        } else {
            machineInfo.setName(nameTextField.getText());
            machineInfo.setRepairTime(((Number)overhaulIdleFTField.getValue()).doubleValue());
            machineInfo.setRepairCosts(((Number)jFTFrepairCosts.getValue()).doubleValue());
            machineInfo.setWorkerCount(((Number)overhaulIntencityFTField.getValue()).intValue());
            machineInfo.setMaintenanceTime(((Number)maintenanceIdleFTField.getValue()).doubleValue());
            machineInfo.setMaintenanceCosts(((Number)jFTFMaintenanceCosts.getValue()).doubleValue());
            machineInfo.setMaintenanceWorkerCount(((Number)maintenanceIntencityFTField.getValue()).intValue());
            
        }
        if(msg == null){
            HibernateUtil.save(machineInfo);
            clear();
        }else{
            JOptionPane.showMessageDialog(this, msg, "Ошибка", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_addButtonActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        RepairSheetJFrame frame = new RepairSheetJFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void clear(){
        nameTextField.setText("");
        typeComboBox.setSelectedIndex(-1);
        intensityFTField.setText("");
        overhaulIdleFTField.setText("");
        overhaulIntencityFTField.setText("");
        overhaulTimeFTField.setText("");
        maintenanceIdleFTField.setText("");
        maintenanceIntencityFTField.setText("");
        maintenanceTimeFTField.setText("");
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JFormattedTextField intensityFTField;
    private javax.swing.JLabel intensityLabel;
    private javax.swing.JLabel intensityMeasureLabel;
    private javax.swing.JFormattedTextField jFTFMaintenanceCosts;
    private javax.swing.JFormattedTextField jFTFrepairCosts;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JFormattedTextField maintenanceIdleFTField;
    private javax.swing.JLabel maintenanceIdleLabel;
    private javax.swing.JFormattedTextField maintenanceIntencityFTField;
    private javax.swing.JLabel maintenanceIntencityLabel;
    private javax.swing.JFormattedTextField maintenanceTimeFTField;
    private javax.swing.JLabel maintenanceTimeLabel;
    private javax.swing.JLabel maintenanceTimeMeasureLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JFormattedTextField overhaulIdleFTField;
    private javax.swing.JLabel overhaulIdleLabel;
    private javax.swing.JFormattedTextField overhaulIntencityFTField;
    private javax.swing.JLabel overhaulIntencityLabel;
    private javax.swing.JFormattedTextField overhaulTimeFTField;
    private javax.swing.JLabel overhaulTimeMeasureLabel;
    private javax.swing.JLabel owerhaulTimeLabel;
    private javax.swing.JComboBox typeComboBox;
    private javax.swing.JLabel typeLabel;
    // End of variables declaration//GEN-END:variables
}
